<?php
/**
 * TomatoCMS
 *
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 *
 * @copyright    Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license        http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version     $Id: User.php 5336 2010-09-07 08:15:47Z huuphuoc $
 * @since        2.0.5
 */

class Bizprofile_Models_Dao_Pdo_Mysql_Customer extends Tomato_Model_Dao
    implements Bizprofile_Models_Interface_Customer
{
    public function convert($entity)
    {
        return new Bizprofile_Models_Customer($entity);
    }

    public function add($customer)
    {
        $this->_conn->insert($this->_prefix . 'customer',
                            array(
                                'user_id'            => $customer->user_id,
                                'customer_address'   => $customer->customer_address,
                                'customer_city'      => $customer->customer_city,
                                'customer_tel'       => $customer->customer_tel,
                                'shipping_name'      => $customer->shipping_name,
                                'shipping_email'     => $customer->shipping_email,
                                'shipping_address'   => $customer->shipping_address,
                                'shipping_city'      => $customer->shipping_city,
                                'shipping_tel'       => $customer->shipping_tel,
                            ));
        return $this->_conn->lastInsertId($this->_prefix . 'customer');
    }

    public function getCustomer($id){
        $select = $this->_conn
                        ->select()
                        ->from(array('c' => $this->_prefix.'customer'))
                        ->limit(1)
                        ->where('c.user_id = ?', $id)
                        ;
        $rowset = $select->query()->fetchAll();
        $customer = new Tomato_Model_Recordset($rowset, $this);
        return (count($customer) == 0) ? null : $customer[0];
    }

    public function updateCustomer($customer){
        $where[] = "user_id = {$customer->user_id}";
        return $this->_conn->update($this->_prefix . 'customer',
                            array(
                                'customer_address'  => $customer->customer_address,
                                'customer_city'     => $customer->customer_city,
                                'customer_tel'      => $customer->customer_tel,
                                'shipping_name'     => $customer->shipping_name,
                                'shipping_email'    => $customer->shipping_email,
                                'shipping_address'  => $customer->shipping_address,
                                'shipping_city'     => $customer->shipping_city,
                                'shipping_tel'      => $customer->shipping_tel,
                            ), $where);
		                                                        		
    }
}
